const express = require('express');
const history = require('connect-history-api-fallback');
const app = express();

const client = require('./routes/client');
const admin = require('./routes/admin');
const api = require('./routes/api');
const path = require('path');

app.use(history({
  rewrites: [
    {
      from: /^\/gftopen\/.*$/,
      to: function (context) {
        return context.parsedUrl.pathname;
      }
    }, {
      from: /^\/upload\/.*$/,
      to: function (context) {
        console.log(context.parsedUrl.pathname);
        return context.parsedUrl.pathname;
      }
    }, {
      from: /^\/dist\/.*$/,
      to: function (context) {
        console.log(context.parsedUrl.pathname);
        return context.parsedUrl.pathname;
      }
    }
  ]
})); // 在express中使用的时候需要注意一点的是，该中间件必须要放在express.static中间前的前面引入，否则会出现问题。
app.use(express.static(path.join(__dirname, '/dist')));
app.use(express.static(path.join(__dirname, '/upload')));

// 客户端
app.use(client);

// 服务端
app.use('/admin', admin);

// 接口
app.use(api);

app.get('*', function (req, res) {
  res.end(' <h1>404</h1>');
});

const port = 3000;
const hostname = '127.0.0.1';
app.listen(port, hostname, function (result) {
  console.log(`Server is run on http://${hostname}:${port}`);
});
